package com.cleaningcloud.cld.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cleaningcloud.cld.domain.CldContract;
import com.cleaningcloud.cld.domain.CldDegreetitleProject;
import com.cleaningcloud.cld.domain.CldProject;
import com.cleaningcloud.cld.domain.dto.CldRegularMeetingDto;
import com.cleaningcloud.cld.domain.vo.CldRegularMeetingVo;
import com.cleaningcloud.cld.mapper.CldContractMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldRegularMeetingMapper;
import com.cleaningcloud.cld.domain.CldRegularMeeting;
import com.cleaningcloud.cld.service.ICldRegularMeetingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 例会管理Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-01-13
 */
@Service
public class CldRegularMeetingServiceImpl implements ICldRegularMeetingService 
{
    @Autowired
    private CldRegularMeetingMapper cldRegularMeetingMapper;

    @Autowired
    private CldContractMapper cldContractMapper;

    /**
     * 查询例会管理
     * 
     * @param regularMeetingId 例会管理主键
     * @return 例会管理
     */
    @Override
    public CldRegularMeetingVo selectCldRegularMeetingByRegularMeetingId(Long regularMeetingId)
    {
        return cldRegularMeetingMapper.selectCldRegularMeetingByRegularMeetingId(regularMeetingId);
    }

    /**
     * 查询例会管理列表
     * 
     * @param cldRegularMeeting 例会管理
     * @return 例会管理
     */
    @Override
    public List<CldRegularMeetingVo> selectCldRegularMeetingList(CldRegularMeeting cldRegularMeeting)
    {
        return cldRegularMeetingMapper.selectCldRegularMeetingList(cldRegularMeeting);
    }

    /**
     * 新增例会管理
     * 
     * @param cldRegularMeeting 例会管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldRegularMeeting(CldRegularMeetingDto cldRegularMeeting)
    {
        cldRegularMeeting.setCreateTime(DateUtils.getNowDate());
        cldRegularMeeting.setCreateBy(SecurityUtils.getUsername());
        cldRegularMeeting.setFlag(0L);
        if(cldRegularMeeting.getProjectId() == null){
            // 类型为全选时
            if (cldRegularMeeting.getProjectType() == 1) {
                // 查询签订中的项目
                CldContract query = new CldContract();
                query.setContractStatus(1L);
                List<CldContract> cldContractList = cldContractMapper.selectCldContractList(query);
                for (CldContract contract : cldContractList) {
                    cldRegularMeeting.setProjectId(contract.getProjectId());
                    cldRegularMeeting.setRegularMeetingId(null);
                    cldRegularMeetingMapper.insertCldRegularMeeting(cldRegularMeeting);
                }
            }else{
                for (CldProject cldProject : cldRegularMeeting.getProjectList()) {
                    cldRegularMeeting.setProjectId(cldProject.getProjectId());
                    cldRegularMeeting.setRegularMeetingId(null);
                    cldRegularMeetingMapper.insertCldRegularMeeting(cldRegularMeeting);
                }
            }
        }else{
            cldRegularMeetingMapper.insertCldRegularMeeting(cldRegularMeeting);
        }
        return 1;
    }

    /**
     * 修改例会管理
     * 
     * @param cldRegularMeeting 例会管理
     * @return 结果
     */
    @Override
    public int updateCldRegularMeeting(CldRegularMeeting cldRegularMeeting)
    {
        return cldRegularMeetingMapper.updateCldRegularMeeting(cldRegularMeeting);
    }

    /**
     * 批量删除例会管理
     * 
     * @param regularMeetingIds 需要删除的例会管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRegularMeetingByRegularMeetingIds(Long[] regularMeetingIds)
    {
        return cldRegularMeetingMapper.deleteCldRegularMeetingByRegularMeetingIds(regularMeetingIds);
    }

    /**
     * 删除例会管理信息
     * 
     * @param regularMeetingId 例会管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRegularMeetingByRegularMeetingId(Long regularMeetingId)
    {
        return cldRegularMeetingMapper.deleteCldRegularMeetingByRegularMeetingId(regularMeetingId);
    }
}
